package com.springboot.util;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import com.springboot.blog.dto.Result;
import com.springboot.blog.entity.Pic;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.InputStream;

@Component
@Data
public class QiNiuYun {
    //静态方法属性不能直接注入 需要通过非静态set方法注入
    private static String accessKey;
    private static String secretKey;
    private static String bucket;
    private static String region;
    private static String host;

    @Value("${qiniu.accessKey}")
    public void setAccessKey(String accessKey) {
        QiNiuYun.accessKey = accessKey;
    }

    @Value("${qiniu.secretKey}")
    public void setSecretKey(String secretKey) {
        QiNiuYun.secretKey = secretKey;
    }

    @Value("${qiniu.bucket}")
    public void setBucket(String bucket) {
        QiNiuYun.bucket = bucket;
    }

    @Value("${qiniu.region}")
    public void setRegion(String region) {
        QiNiuYun.region = region;
    }

    public static Region getRegion() {
        switch (region) {
            case "huadong":
                return Region.huadong();
            case "huabei":
                return Region.huabei();
            case "huanan":
                return Region.huanan();
            case "beimei":
                return Region.beimei();
            case "xinjiapo":
                return Region.xinjiapo();
            default: //可选
                return Region.autoRegion();
        }
    }

    public static UploadManager getUploadManager() {
        Configuration cfg = new Configuration(getRegion());
        return new UploadManager(cfg);
    }

    public static StringMap getPutPolicy() {
        StringMap putPolicy = new StringMap();
        putPolicy.put("returnBody", "{\"url\":\"$(key)\",\"hash\":\"$(etag)\",\"mimeType\":\"$(mimeType)\",\"fsize\":$(fsize)}");
        return putPolicy;
    }

    //InputStream对象的上传
    public static Result<Pic> uploadImg(InputStream is, String key) {
        UploadManager uploadManager = getUploadManager();
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket, key, 3600, getPutPolicy());
        Result<Pic> result = new Result<>();
        try {
            Response response = uploadManager.put(is, key, upToken, null, null);
            String bodyString = response.bodyString();
            Pic pic = new Gson().fromJson(bodyString, Pic.class);
            if (StringUtils.isEmpty(pic.getUrl())) {
                result.error("请求成功!转换出错!" + bodyString);
            } else {
                result.success("获取成功").data(new Gson().fromJson(bodyString, Pic.class));
            }
        } catch (QiniuException ex) {
            result.error(ex.response.error);
        }
        return result;
    }
}